package com.bbznb.service.serviceImpl;

import com.bbznb.entity.UserData;
import com.bbznb.mapper.UserDataMapper;
import com.bbznb.service.UserDataService;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import static com.bbznb.common.UserS.USER_LOGIN;

@Service
@Slf4j
public class UserDataServiceImpl  implements UserDataService {

    @Autowired
    private  MinIOService minIOService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserDataMapper userDataMapper;
    @Override
    public UserData getUserData() {
        UserData userData = userDataMapper.selectByPrimaryKey(redisTemplate.opsForValue().get(USER_LOGIN));
        UserData userData1 = userDataMapper.select(userData.getUserId());
        userData.setAge(userData1.getAge());
        userData.setMobite( userDataMapper.selectp(userData.getUsername()));
        userData.setAvatarImg(userData1.getAvatarImg());
        userData.setBalance(userData1.getBalance());
        return  userData;
    }

    @Override
    public void updateUserData(Integer age, MultipartFile file) {
        MinioClient minioClient = MinioClient.builder().
                    endpoint("http://192.168.254.1:9000").credentials("minioadmin","minioadmin").build();
        String bucketName = "bbznb-bucket";
        minIOService.SetMinIOService(minioClient,bucketName);
        System.out.println("file:"+file);
        System.out.println("age:"+age);
        try {
            String people_url = minIOService.uploadImage(file);
            String username = redisTemplate.opsForValue().get(USER_LOGIN);
                Long userId = userDataMapper.selectByPrimaryKey(username).getUserId();
            userDataMapper.updateimg(age,people_url,userId);

        } catch (Exception e) {
                e.printStackTrace();
        }

    }
}
